package org.example.node;

import com.alibaba.fastjson.JSON;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

public class IndustryUtil {

    private static Integer pkId=1;

    private static Integer high=0;

    public static Map<String,EleNode> map=new HashMap<String,EleNode>();

    public static EleNode root=new EleNode();

    public static String json="";

    public static void main(String[] args) {
        List<String> hotKw = getHotKw("吊灯");
        hotKw.stream().forEach(System.out::println);
    }

    static {
        SAXReader saxReader = new SAXReader();
        Document xmlDocument = null;
        try {
            InputStream resourceAsStream = IndustryUtil.class.getClassLoader().getResourceAsStream("txt/industry-graph.xml");
            //xmlDocument = saxReader.read(new FileInputStream(new File("D:\\yfb202116\\yfb_wechat\\src\\main\\resources\\txt\\industry-graph.xml")));
            xmlDocument = saxReader.read(resourceAsStream);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        Element rootElement = xmlDocument.getRootElement();
        root.setHigh(high);
        root.setId(pkId);
        root.setNodeName("root节点");
        root.setPId(0);
        root.setChildEleNode(null);
        List<EleNode> ele = getEle(rootElement, root,high,"");
        root.setChildEleNode(ele);
        json= JSON.toJSONString(root);
        listTransferToLink(root);
    }

    public static List<String> getHotKw(String kw){
        EleNode eleNode = map.get(kw);
        List<String> list = new ArrayList<>();
        list.addAll(eleNode.getLeafData());
        EleNode pre=null;
        EleNode next=null;
        if(eleNode.pre!=null){
            if(CollectionUtils.isNotEmpty(eleNode.pre.getLeafData())){
                pre=eleNode.pre;
            }else{
                pre=eleNode.pre.getChildEleNode().get(eleNode.pre.getChildEleNode().size()-1);
            }
        }
        if(eleNode.next!=null ){
            if(CollectionUtils.isNotEmpty(eleNode.next.getLeafData())){
                next=eleNode.next;
            }else{
                next=eleNode.next.getChildEleNode().get(0);
            }
        }
        return searchHotKw(pre,next, list);
    }

    public static List<EleNode> getEle(Element element, EleNode root,Integer high,String pName){
        List<Element> elements = element.elements("node");
        if(CollectionUtils.isEmpty(elements)){
            return null;
        }
        List<EleNode> eleNodes=new ArrayList<>();
        //设置高度
        ++high;
        for (Element e:elements) {
            EleNode eleNode = getEleNodeByElement(e);
            eleNode.setId(++pkId);
            eleNode.setPId(root.getId());
            eleNode.setHigh(high);
            if(StringUtils.isNotEmpty(pName)){
                eleNode.setNodeName(pName+">"+eleNode.getNodeName());
            }
            List<EleNode> childEle = getEle(e, eleNode,high,eleNode.getNodeName());
            eleNode.setChildEleNode(childEle);
            eleNodes.add(eleNode);
        }
        return eleNodes;
    }

    public static void listTransferToLink(EleNode root){
        if(root!=null){
            List<EleNode> childEle = root.getChildEleNode();
            if(CollectionUtils.isNotEmpty(childEle)){
                EleNode pre=null;
                for (int i = 0; i < childEle.size(); i++) {
                    EleNode tempNode = childEle.get(i);
                    if(pre!=null){
                        tempNode.setPre(pre);
                    }
                    pre=tempNode;
                    if((i+1)<childEle.size()){
                        tempNode.setNext(childEle.get(i+1));
                    }
                    List<String> leafData = tempNode.getLeafData();
                    if(CollectionUtils.isNotEmpty(leafData)){
                        for (int j = 0; j < leafData.size(); j++) {
                            String s = leafData.get(j);
                            map.put(s,tempNode);
                        }
                    }
                    listTransferToLink(tempNode);
                }
            }
        }
    }

    public static EleNode getEleNodeByElement(Element element){
        EleNode eleNode = new EleNode();
        eleNode.setNodeName(element.attribute("name").getValue());
        String text = element.getTextTrim();
        if(StringUtils.isNotEmpty(text)){
            String[] split = text.split(",");
            eleNode.setLeafData(Arrays.stream(split).map(String::trim).collect(Collectors.toList()));
        }
        return eleNode;
    }


    public static List<String> searchHotKw(EleNode pre,EleNode next, List<String> list) {
        while (!(pre==null && next==null)){
            if(list.size()>=8){
                break;
            }
            if(pre!=null){
                if(list.size()<8){
                    List<String> preLeafData = pre.getLeafData();
                    if(CollectionUtils.isNotEmpty(preLeafData)){
                        String s = Optional.ofNullable(preLeafData).orElse(new ArrayList<>()).get(0);
                        list.add(s);
                        pre=pre.pre;
                    }else{
                        pre=pre.getChildEleNode().get(pre.getChildEleNode().size()-1);
                    }
                }
            }
            if(next!=null){
                if(list.size()<8){
                    List<String> nextLeafData = next.getLeafData();
                    if(CollectionUtils.isNotEmpty(nextLeafData)){
                        String s = Optional.ofNullable(nextLeafData).orElse(new ArrayList<>()).get(0);
                        list.add(s);
                        next=next.next;
                    }else{
                        next=next.getChildEleNode().get(0);
                    }
                }
            }
        }
        return list;
    }
}
